$(function () {

	$("#jqGrid").jqGrid({
		url: '../tpurviewusermanage/list',
		datatype: "json",
		colModel: [			
			{ label: '公司名称', name: 'corpname', index: 'corpname', width: 80 }, 			
			{ label: '用户编码', name: 'number', index: 'number', width: 80 }, 			
			{ label: '系统账号', name: 'username', index: 'username', width: 80 }, 			
			{ label: '用户姓名', name: 'name', index: 'name', width: 80 }, 			
			{ label: '性别', name: 'gender', width: 80, formatter: function(value, options, row){
				return value == 0 ? 
				'<span class="label label-default">男</span>' : 
				'<span class="label label-default">女</span>';
			}},		
			{ label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
				return value == 0 ? 
				'<span class="label label-success">正常</span>':
				'<span class="label label-danger">禁用</span>' ;
			}}
		],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true, 
		rownumWidth: 25, 
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page", 
			rows:"limit", 
			order: "order"
		},
		gridComplete:function(){
    	//隐藏grid底部滚动条
    	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
    }
  });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tPurviewUsermanage: {},
		rowPwd: {
			password: '',
			password2: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tPurviewUsermanage = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var legalObj = {
				number: true,
				username: true,
				name: true,
				gender: true,
				password: true,
				password2: true
			};
			// 非空校验
			if(!oneRowIsLegal(vm.tPurviewUsermanage,legalObj)){
				alert('请先输入必填项');
				return;
			}
			if(vm.tPurviewUsermanage.password !== vm.tPurviewUsermanage.password2){
				alert('两次输入密码不一致');
				return;
			}
			var url = vm.tPurviewUsermanage.id == null ? "../tpurviewusermanage/save" : "../tpurviewusermanage/update";
			$.ajax({
				type: "POST",
				url: url,
				contentType: "application/json",
				data: JSON.stringify(vm.tPurviewUsermanage),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
					url: "../tpurviewusermanage/delete",
					contentType: "application/json",
					data: JSON.stringify(ids),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		/**
		 * [updateStatus 更新状态]
		 * @param     {[status]}                 status [1：启用;  0：禁用]
		 * @return    {[type]}                        [description]
		 */
		 updateStatus: function (status) {
		 	var id = getSelectedRow();
		 	if(id == null){
		 		return ;
		 	}
		 	var url = "../tpurviewusermanage/update";
		 	var data = {
		 		id: id,
		 		status: status
		 	}
		 	$.ajax({
		 		type: "POST",
		 		url: url,
		 		contentType: "application/json",
		 		data: JSON.stringify(data),
		 		success: function(r){
		 			if(r.code == 0){
		 				alert('操作成功', function(index){
		 					$("#jqGrid").trigger("reloadGrid");
		 				});
		 			}else{
		 				alert(r.msg);
		 			}	
		 		}
		 	});
		 },
		 getInfo: function(id){
		 	$.get("../tpurviewusermanage/info/"+id, function(r){
		 		vm.tPurviewUsermanage = r.tPurviewUsermanage;
				// 确认密码赋值
				vm.tPurviewUsermanage.password2 = vm.tPurviewUsermanage.password;
			});
		 },
		 reload: function (event) {
		 	vm.showList = true;
		 	var page = $("#jqGrid").jqGrid('getGridParam','page');
		 	$("#jqGrid").jqGrid('setGridParam',{ 
		 		page:page
		 	}).trigger("reloadGrid");
		 },
		 resetPwd: function(){
		 	var id = getSelectedRow();
		 	if(id == null){
		 		return ;
		 	}
		 	// 重置为空
		 	vm.rowPwd = {
		 		password: '',
		 		password2: ''
		 	}
		 	layer.open({
		 		type: 1,
		 		skin: 'layui-layer-molv',
		 		title: "重置密码",
		 		area: ['550px', '270px'],
		 		shadeClose: false,
		 		content: jQuery("#passwordResetLayer"),
		 		btn: ['确定','取消'],
		 		btn1: function (index) {
		 			var legalObj = {
		 				password: true,
		 				password2: true
		 			};
					// 非空校验
					if(!oneRowIsLegal(vm.rowPwd,legalObj)){
						alert('请先输入必填项');
						return;
					}
					if(vm.rowPwd.password !== vm.rowPwd.password2){
						alert('两次输入密码不一致');
						return;
					}
					var url = "../tpurviewusermanage/update";
					var data = {
						id: id,
						password: vm.rowPwd.password
					}
					$.ajax({
						type: "POST",
						url: url,
						contentType: "application/json",
						data: JSON.stringify(data),
						success: function(r){
							if(r.code == 0){
								alert('操作成功', function(index){
									$("#jqGrid").trigger("reloadGrid");
								});
							}else{
								alert(r.msg);
							}	
						}
					});
				}
			});
		 }
		}
	});